﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using nspLib;
using nspLib.Util;
using NextStep_V1._0.Model.Util;
using System.Data.SqlClient;
using NextStep_V1._0.View.Control;

namespace NextStep_V1._0
{
    public partial class frmMain : Forms
    {
        MetrolBillCommands _Commands = null;
        
        public frmMain()
        {
            InitializeComponent();
        }

        private void exit_Click(object sender, EventArgs e)
        {
            DialogResult results;
            results = MessageBox.Show("Bạn có muốn sao lưu lại dữ liệu trước khi thoát không?", 
                "Thoát ứng dụng!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
            if (results == DialogResult.Yes)
            {

                String strName = new CommonUtils().AutoStrName();
                saveFileDialogBackup.FileName = "Backup" + strName + ".bak";
                saveFileDialogBackup.Filter = "Backup File (*.bak)|*.bak";
                saveFileDialogBackup.Title = "File location";
                saveFileDialogBackup.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                if (saveFileDialogBackup.ShowDialog() == DialogResult.OK)
                {
                    new DBUtils().BackupBAK("QUANLYKHO", saveFileDialogBackup.FileName);
                    MessageBox.Show("Sao lưu dữ liệu thành công!", "Sao lưu cơ sở dữ liệu", MessageBoxButtons.OK);
                    this.Close();
                }
            }
            else if (results == DialogResult.No)
            {
                this.Close();
            }
        }

        private void RestoreDB_Click(object sender, EventArgs e)
        {

        }

        private void helpContent_Click(object sender, EventArgs e)
        {

        }

        private void websiteNSP_Click(object sender, EventArgs e)
        {

        }

        private void teamView_Click(object sender, EventArgs e)
        {

        }

        private void about_Click(object sender, EventArgs e)
        {
            frmAbout about = new frmAbout();
            about.ShowDialog();
        }

        private void frmMain_Load(object sender, EventArgs e)
        {

            frmLogin login = new frmLogin();
            menuList.Visible = false;
            menuService.Visible = false;
            menuSystem.Visible = false;
            menuUtilities.Visible = false;
            toolBar.Visible = false;
            paneMenu.Visible = false;
            
            login.ShowDialog();

            //kiem tra xem 
            Boolean checkNullBase = new DBUtils().getBaseUnit();
            if (checkNullBase == true)
            {
                ctrlSetupFirstBase ctr = new ctrlSetupFirstBase();
                this.paneShowMain.Controls.Add(ctr);
            }
        }

        protected override void OnFormClosing(FormClosingEventArgs e)
        {
            base.OnFormClosing(e);
            if (e.CloseReason == CloseReason.WindowsShutDown) return;
                DialogResult results;
                results = MessageBox.Show("Bạn có muốn sao lưu lại dữ liệu trước khi thoát không?",
                    "Thoát ứng dụng!", MessageBoxButtons.YesNoCancel, MessageBoxIcon.Question);
                if (results == DialogResult.Yes)
                {

                    String strName = new CommonUtils().AutoStrName();
                    saveFileDialogBackup.FileName = "Backup" + strName + ".bak";
                    saveFileDialogBackup.Filter = "Backup File (*.bak)|*.bak";
                    saveFileDialogBackup.Title = "File location";
                    saveFileDialogBackup.InitialDirectory = AppDomain.CurrentDomain.BaseDirectory;
                    if (saveFileDialogBackup.ShowDialog() == DialogResult.OK)
                    {
                        new DBUtils().BackupBAK("QUANLYKHO", saveFileDialogBackup.FileName);
                        MessageBox.Show("Sao lưu dữ liệu thành công!", "Sao lưu cơ sở dữ liệu", MessageBoxButtons.OK);
                        e.Cancel = false;
                    }
                }
                else if (results == DialogResult.Cancel)
                {
                    e.Cancel = true;
                }
        }

    }
}
